Graphviz CheatSheet

sudo apt-get install graphviz

Examples from: https://github.com/xflr6/graphviz/tree/master/examples


In [2]:
from graphviz import Digraph

dot = Digraph(comment='The Round Table')

dot.node('A', 'King Arthur')
dot.node('B', 'Sir Bedevere the Wise')
dot.node('L', 'Sir Lancelot the Brave')

dot.edges(['AB', 'AL'])
dot.edge('B', 'L', constraint='false')

dot


Out[2]:
%3 A King Arthur B Sir Bedevere the Wise A->B L Sir Lancelot the Brave A->L B->L

In [3]:
from graphviz import Source

src = Source('digraph "the holy hand grenade" { rankdir=LR; 1 -> 2 -> 3 -> lob }')
src


Out[3]:
the holy hand grenade 1 1 2 2 1->2 3 3 2->3 lob lob 3->lob

In [4]:
# http://www.graphviz.org/content/fsm

from graphviz import Digraph

f = Digraph('finite_state_machine', filename='fsm.gv')
f.attr(rankdir='LR', size='8,5')

f.attr('node', shape='doublecircle')
f.node('LR_0')
f.node('LR_3')
f.node('LR_4')
f.node('LR_8')

f.attr('node', shape='circle')
f.edge('LR_0', 'LR_2', label='SS(B)')
f.edge('LR_0', 'LR_1', label='SS(S)')
f.edge('LR_1', 'LR_3', label='S($end)')
f.edge('LR_2', 'LR_6', label='SS(b)')
f.edge('LR_2', 'LR_5', label='SS(a)')
f.edge('LR_2', 'LR_4', label='S(A)')
f.edge('LR_5', 'LR_7', label='S(b)')
f.edge('LR_5', 'LR_5', label='S(a)')
f.edge('LR_6', 'LR_6', label='S(b)')
f.edge('LR_6', 'LR_5', label='S(a)')
f.edge('LR_7', 'LR_8', label='S(b)')
f.edge('LR_7', 'LR_5', label='S(a)')
f.edge('LR_8', 'LR_6', label='S(b)')
f.edge('LR_8', 'LR_5', label='S(a)')

f


Out[4]:
finite_state_machine LR_0 LR_0 LR_2 LR_2 LR_0->LR_2 SS(B) LR_1 LR_1 LR_0->LR_1 SS(S) LR_3 LR_3 LR_4 LR_4 LR_8 LR_8 LR_6 LR_6 LR_8->LR_6 S(b) LR_5 LR_5 LR_8->LR_5 S(a) LR_2->LR_4 S(A) LR_2->LR_6 SS(b) LR_2->LR_5 SS(a) LR_1->LR_3 S($end) LR_6->LR_6 S(b) LR_6->LR_5 S(a) LR_5->LR_5 S(a) LR_7 LR_7 LR_5->LR_7 S(b) LR_7->LR_8 S(b) LR_7->LR_5 S(a)

In [5]:
# http://www.graphviz.org/pdf/dotguide.pdf Figure 20

from graphviz import Digraph

g = Digraph('G', filename='cluster_edge.gv')
g.attr(compound='true')

with g.subgraph(name='cluster0') as c:
    c.edges(['ab', 'ac', 'bd', 'cd'])

with g.subgraph(name='cluster1') as c:
    c.edges(['eg', 'ef'])

g.edge('b', 'f', lhead='cluster1')
g.edge('d', 'e')
g.edge('c', 'g', ltail='cluster0', lhead='cluster1')
g.edge('c', 'e', ltail='cluster0')
g.edge('d', 'h')

g


Out[5]:
G cluster0 cluster1 a a b b a->b c c a->c d d b->d f f b->f c->d e e c->e g g c->g d->e h h d->h e->g e->f

In [8]:
from graphviz import Digraph

g = Digraph('G', filename='hello.gv')

g.edge('Hello', 'World')

g


Out[8]:
G Hello Hello World World Hello->World

In [9]:
# angles.py - http://www.graphviz.org/Gallery/gradient/angles.html

from graphviz import Digraph

g = Digraph('G', filename='angles.gv')
g.attr(bgcolor='blue')

with g.subgraph(name='cluster_1') as c:
    c.attr(fontcolor='white')
    c.attr('node', shape='circle', style='filled', fillcolor='white:black',
           gradientangle='360', label='n9:360', fontcolor='black')
    c.node('n9')
    for i, a in zip(range(8, 0, -1), range(360 - 45, -1, -45)):
        c.attr('node', gradientangle='%d' % a, label='n%d:%d' % (i, a))
        c.node('n%d' % i)
    c.attr(label='Linear Angle Variations (white to black gradient)')

with g.subgraph(name='cluster_2') as c:
    c.attr(fontcolor='white')
    c.attr('node', shape='circle', style='radial', fillcolor='white:black',
           gradientangle='360', label='n18:360', fontcolor='black')
    c.node('n18')
    for i, a in zip(range(17, 9, -1), range(360 - 45, -1, -45)):
        c.attr('node', gradientangle='%d' % a, label='n%d:%d' % (i, a))
        c.node('n%d' % i)
    c.attr(label='Radial Angle Variations (white to black gradient)')

g.edge('n5', 'n14')

g


Out[9]:
G cluster_1 Linear Angle Variations (white to black gradient) cluster_2 Radial Angle Variations (white to black gradient) n9 n9:360 n8 n8:315 n7 n7:270 n6 n6:225 n5 n5:180 n14 n14:180 n5->n14 n4 n4:135 n3 n3:90 n2 n2:45 n1 n1:0 n18 n18:360 n17 n17:315 n16 n16:270 n15 n15:225 n13 n13:135 n12 n12:90 n11 n11:45 n10 n10:0

In [10]:
# unix.py - http://www.graphviz.org/content/unix

from graphviz import Digraph

u = Digraph('unix', filename='unix.gv')
u.attr(size='6,6')
u.node_attr.update(color='lightblue2', style='filled')

u.edge('5th Edition', '6th Edition')
u.edge('5th Edition', 'PWB 1.0')
u.edge('6th Edition', 'LSX')
u.edge('6th Edition', '1 BSD')
u.edge('6th Edition', 'Mini Unix')
u.edge('6th Edition', 'Wollongong')
u.edge('6th Edition', 'Interdata')
u.edge('Interdata', 'Unix/TS 3.0')
u.edge('Interdata', 'PWB 2.0')
u.edge('Interdata', '7th Edition')
u.edge('7th Edition', '8th Edition')
u.edge('7th Edition', '32V')
u.edge('7th Edition', 'V7M')
u.edge('7th Edition', 'Ultrix-11')
u.edge('7th Edition', 'Xenix')
u.edge('7th Edition', 'UniPlus+')
u.edge('V7M', 'Ultrix-11')
u.edge('8th Edition', '9th Edition')
u.edge('1 BSD', '2 BSD')
u.edge('2 BSD', '2.8 BSD')
u.edge('2.8 BSD', 'Ultrix-11')
u.edge('2.8 BSD', '2.9 BSD')
u.edge('32V', '3 BSD')
u.edge('3 BSD', '4 BSD')
u.edge('4 BSD', '4.1 BSD')
u.edge('4.1 BSD', '4.2 BSD')
u.edge('4.1 BSD', '2.8 BSD')
u.edge('4.1 BSD', '8th Edition')
u.edge('4.2 BSD', '4.3 BSD')
u.edge('4.2 BSD', 'Ultrix-32')
u.edge('PWB 1.0', 'PWB 1.2')
u.edge('PWB 1.0', 'USG 1.0')
u.edge('PWB 1.2', 'PWB 2.0')
u.edge('USG 1.0', 'CB Unix 1')
u.edge('USG 1.0', 'USG 2.0')
u.edge('CB Unix 1', 'CB Unix 2')
u.edge('CB Unix 2', 'CB Unix 3')
u.edge('CB Unix 3', 'Unix/TS++')
u.edge('CB Unix 3', 'PDP-11 Sys V')
u.edge('USG 2.0', 'USG 3.0')
u.edge('USG 3.0', 'Unix/TS 3.0')
u.edge('PWB 2.0', 'Unix/TS 3.0')
u.edge('Unix/TS 1.0', 'Unix/TS 3.0')
u.edge('Unix/TS 3.0', 'TS 4.0')
u.edge('Unix/TS++', 'TS 4.0')
u.edge('CB Unix 3', 'TS 4.0')
u.edge('TS 4.0', 'System V.0')
u.edge('System V.0', 'System V.2')
u.edge('System V.2', 'System V.3')

u


Out[10]:
unix 5th Edition 5th Edition 6th Edition 6th Edition 5th Edition->6th Edition PWB 1.0 PWB 1.0 5th Edition->PWB 1.0 LSX LSX 6th Edition->LSX 1 BSD 1 BSD 6th Edition->1 BSD Mini Unix Mini Unix 6th Edition->Mini Unix Wollongong Wollongong 6th Edition->Wollongong Interdata Interdata 6th Edition->Interdata PWB 1.2 PWB 1.2 PWB 1.0->PWB 1.2 USG 1.0 USG 1.0 PWB 1.0->USG 1.0 2 BSD 2 BSD 1 BSD->2 BSD Unix/TS 3.0 Unix/TS 3.0 Interdata->Unix/TS 3.0 PWB 2.0 PWB 2.0 Interdata->PWB 2.0 7th Edition 7th Edition Interdata->7th Edition TS 4.0 TS 4.0 Unix/TS 3.0->TS 4.0 PWB 2.0->Unix/TS 3.0 8th Edition 8th Edition 7th Edition->8th Edition 32V 32V 7th Edition->32V V7M V7M 7th Edition->V7M Ultrix-11 Ultrix-11 7th Edition->Ultrix-11 Xenix Xenix 7th Edition->Xenix UniPlus+ UniPlus+ 7th Edition->UniPlus+ 9th Edition 9th Edition 8th Edition->9th Edition 3 BSD 3 BSD 32V->3 BSD V7M->Ultrix-11 2.8 BSD 2.8 BSD 2 BSD->2.8 BSD 2.8 BSD->Ultrix-11 2.9 BSD 2.9 BSD 2.8 BSD->2.9 BSD 4 BSD 4 BSD 3 BSD->4 BSD 4.1 BSD 4.1 BSD 4 BSD->4.1 BSD 4.1 BSD->8th Edition 4.1 BSD->2.8 BSD 4.2 BSD 4.2 BSD 4.1 BSD->4.2 BSD 4.3 BSD 4.3 BSD 4.2 BSD->4.3 BSD Ultrix-32 Ultrix-32 4.2 BSD->Ultrix-32 PWB 1.2->PWB 2.0 CB Unix 1 CB Unix 1 USG 1.0->CB Unix 1 USG 2.0 USG 2.0 USG 1.0->USG 2.0 CB Unix 2 CB Unix 2 CB Unix 1->CB Unix 2 USG 3.0 USG 3.0 USG 2.0->USG 3.0 CB Unix 3 CB Unix 3 CB Unix 2->CB Unix 3 Unix/TS++ Unix/TS++ CB Unix 3->Unix/TS++ PDP-11 Sys V PDP-11 Sys V CB Unix 3->PDP-11 Sys V CB Unix 3->TS 4.0 Unix/TS++->TS 4.0 USG 3.0->Unix/TS 3.0 Unix/TS 1.0 Unix/TS 1.0 Unix/TS 1.0->Unix/TS 3.0 System V.0 System V.0 TS 4.0->System V.0 System V.2 System V.2 System V.0->System V.2 System V.3 System V.3 System V.2->System V.3

In [12]:
from graphviz import Digraph

g = Digraph('G', filename='cluster.gv')

# NOTE: the subgraph name needs to begin with 'cluster' (all lowercase)
#       so that Graphviz recognizes it as a special cluster subgraph

with g.subgraph(name='cluster_0') as c:
    c.attr(style='filled')
    c.attr(color='lightgrey')
    c.node_attr.update(style='filled', color='white')
    c.edges([('a0', 'a1'), ('a1', 'a2'), ('a2', 'a3')])
    c.attr(label='process #1')

with g.subgraph(name='cluster_1') as c:
    c.node_attr.update(style='filled')
    c.edges([('b0', 'b1'), ('b1', 'b2'), ('b2', 'b3')])
    c.attr(label='process #2')
    c.attr(color='blue')

g.edge('start', 'a0')
g.edge('start', 'b0')
g.edge('a1', 'b3')
g.edge('b2', 'a3')
g.edge('a3', 'a0')
g.edge('a3', 'end')
g.edge('b3', 'end')

g.node('start', shape='Mdiamond')
g.node('end', shape='Msquare')

g


Out[12]:
G cluster_0 process #1 cluster_1 process #2 a0 a0 a1 a1 a0->a1 a2 a2 a1->a2 b3 b3 a1->b3 a3 a3 a2->a3 a3->a0 end end a3->end b0 b0 b1 b1 b0->b1 b2 b2 b1->b2 b2->a3 b2->b3 b3->end start start start->a0 start->b0

In [ ]: